/*
 * Copyright 2018 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.panwenliang2008.eatwhat.utilities

import android.content.Context
import androidx.fragment.app.Fragment
import com.panwenliang2008.eatwhat.data.FoodRepository
import com.panwenliang2008.eatwhat.data.database.AppDatabase
import com.panwenliang2008.eatwhat.ui.select.SelectViewModelFactory
import com.panwenliang2008.eatwhat.ui.selectoption.SelectOptionViewModelFactory

/**
 * Static methods used to inject classes needed for various Activities and Fragments.
 */
object InjectorUtils {

    private fun getFoodRepository(context: Context): FoodRepository {
        return FoodRepository.getInstance(
            AppDatabase.getInstance(context.applicationContext).foodDao()
        )
    }

    fun provideSelectViewModelFactory(fragment: Fragment): SelectViewModelFactory {
        val repository = getFoodRepository(fragment.requireContext())
        return SelectViewModelFactory(
            repository,
            fragment
        )
    }

    fun provideSelectOptionViewModelFactory(fragment: Fragment): SelectOptionViewModelFactory {
        val repository = getFoodRepository(fragment.requireContext())
        return SelectOptionViewModelFactory(
            repository,
            fragment
        )
    }
}
